package com.speedchina.doormanager.controller;


import com.speedchina.doormanager.domain.DoorPlateLog;
import com.speedchina.doormanager.dto.TaskDTO;
import com.speedchina.doormanager.enums.TaskTypeEnum;
import com.speedchina.doormanager.service.DoorPlateLogService;
import com.speedchina.doormanager.dto.DoorPlateTaskDTO;
import com.speedchina.doormanager.service.DoorPlateService;
import com.speedchina.doormanager.service.TaskService;
import com.speedchina.doormanager.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 巡检
 */
@Controller
@RequestMapping("/xunjian")
public class XunJianController {

    @Autowired
    private DoorPlateLogService doorPlateLogService;

    @Autowired
    private TaskService taskService;


    @Autowired
    private DoorPlateService doorPlateService;

    @GetMapping("")
    public String xunjianPage(Model model) {
        List<TaskDTO> taskDTOS = taskService.findAllTaskByTaskType(TaskTypeEnum.XUNJIAN.getStatus());
        model.addAttribute("taskDTOS", taskDTOS);

        return "fenye/xunjian";
    }

    @GetMapping("/list/{taskId}")
    public String xunjianTasksPage(@PathVariable("taskId") String taskId, Model model) {
        DoorPlateTaskDTO doorPlateTaskDTO = taskService.findTaskDTOById(taskId);
        model.addAttribute("doorPlateTaskDTO", doorPlateTaskDTO);
        return "fenye/xj-detaillist";
    }

    @GetMapping("/list/{taskId}/{doorPlateId}")
    public String getXunJianPageById(@PathVariable("taskId") String taskId,
                                     @PathVariable("doorPlateId") String doorPlateId,
                                     Model model) {
        model.addAttribute("taskId", taskId);
        model.addAttribute("doorPlateId", doorPlateId);
        model.addAttribute("doorPlate", doorPlateService.findById(doorPlateId));
        return "fenye/xj-detail";
    }


    @GetMapping("/faqi")
    public String faqiPage() {
        return "fenye/xj-faqi";

    }

    /**
     * 发起门牌巡检
     *
     * @param doorPlateLog
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultVO faqiXunjian(DoorPlateLog doorPlateLog) {
        doorPlateLogService.saveDoorPlateLog(doorPlateLog, TaskTypeEnum.XUNJIAN.getStatus());
        return ResultVO.success();
    }

}
